import {createSlice} from '@reduxjs/toolkit'

//创建 slice 对象. 切片对象
const slice = createSlice({
  //别名设置  action.type 
  name: 'count',
  //设置状态的初始值
  initialState: 100,
  //设置 reducer 的功能
  reducers: {
    // {type: 'incre', payload: 1}
    incre: (state, action) => {
      return state + action.payload;
    },
    // {type: 'decre', payload: 1}
    decre: (state, action) => {
      return state - action.payload;
    }
  }
});

//获得 slice 中的 action creator
export let {incre, decre} = slice.actions;

//声明一个异步的 action creator
export function asyncIncreAction(payload){
  return dispatch => {
    setTimeout(() => {
      dispatch(incre(payload));
    }, 2000)
  }
}

export default slice.reducer;